/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.CategoryEntity;
import cn.eclass.entity.Entity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class CategoryDao extends CommonDao {

    public CategoryDao() {
        super(CategoryDao.class.getName());
    }
    protected static final String TABLENAME = "category";//mysql table name is case sensitive on linux but not on windows;

    public CategoryEntity getById(int id) {
        List<CategoryEntity> categorys = new ArrayList<CategoryEntity>();
        CategoryEntity categoryBean = null;
        String sql = "select * from " + TABLENAME + " where id = " + id;
        categorys = (List<CategoryEntity>) select(sql);
        if (categorys.size() > 0) {
            categoryBean = (CategoryEntity) categorys.get(0);
        }
        return categoryBean;
    }

    public List<CategoryEntity> getByParentId(int parentId) {
        List<CategoryEntity> categorys = new ArrayList<CategoryEntity>();
        String sql = "select * from category where `parent-id` = " + parentId;
        categorys = (List<CategoryEntity>) select(sql);
        return categorys;
    }

    public List<CategoryEntity> getList(/*
             * Pagination
             */) {
        List<CategoryEntity> categorys = new ArrayList<CategoryEntity>();
        String sql = "select * from category";
        categorys = (List<CategoryEntity>) select(sql);
        return categorys;
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        CategoryEntity categoryEntity = new CategoryEntity();
        try {
            categoryEntity.setId(resultSet.getInt("ID"));
            categoryEntity.setParentId((resultSet.getInt("PARENT-ID")));
            categoryEntity.setName(resultSet.getString("NAME"));
            categoryEntity.setDescription(resultSet.getString("DESCRIPTION"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return categoryEntity;
    }

    public void update(CategoryEntity category) {
        String sql = "update category set "
                + " `name` = '" + category.getName() + "',"
                + " description = '" + category.getDescription() + "'"
                + " where id = " + category.getId();
        update(sql);
    }

    public CategoryEntity insert(CategoryEntity category) {
        String sql = "insert into category (`name`,`description`,`parent-id`) values "
                + "('" + category.getName() + "','" + category.getName() + "'," + category.getParentId() + ")";
        int key = super.insert(sql);
        category.setId(key);
        return category;
    }

    public void remove(Integer id) {
        String sql = "delete from category where id = " + id;
        super.delete(sql);
        List<CategoryEntity> children = getByParentId(id);
        if (children.size() > 0) {
            for (CategoryEntity child : children) {
                remove(child.getId());
            }
        }
    }

    public CategoryEntity getByName(String name) {
        List<CategoryEntity> categorys = new ArrayList<CategoryEntity>();
        CategoryEntity category = null;
        String sql = "select * from " + TABLENAME + " where name = '" + name + "'";
        categorys = (List<CategoryEntity>) select(sql);
        if (categorys.size() > 0) {
            category = (CategoryEntity) categorys.get(0);
        }
        return category;
    }
}